package com.ruoyi.web.controller.front;

import cn.hutool.core.net.URLDecoder;
import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.system.domain.vo.HourlyOrderDistributionVO;
import com.ruoyi.system.domain.vo.HourlyRevenueDistributionVO;
import com.ruoyi.system.service.ITbOrderStatisticsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.nio.charset.Charset;
import java.util.List;

/**
 * 数据统计接口
 */
@Api(tags = "数据统计接口")
@RestController
@RequestMapping("/api/statistics")
public class StatisticsController extends BaseController {

    @Autowired
    private ITbOrderStatisticsService orderStatisticsService;

    @ApiOperation(value = "获取今日统计数据")
    @GetMapping("/getTodayStatistics")
    public AjaxResult getTodayStatistics() {
        String companyName=null;
        Object company = ServletUtils.getRequest().getHeader("company");
        if(ObjectUtil.isNotNull(company))
        {
            companyName=URLDecoder.decode(company.toString(), Charset.defaultCharset());
        }
        return AjaxResult.success(orderStatisticsService.getTodayStatistics(companyName));
    }

    @ApiOperation(value = "获取营收趋势数据")
    @GetMapping("/getRevenueTrend")
    public AjaxResult getRevenueTrend(
            @ApiParam(value = "类型：7-近7天，30-近30天", required = true)
            @RequestParam(value = "type", defaultValue = "7") Integer type) {

        String companyName=null;
        Object company = ServletUtils.getRequest().getHeader("company");
        if(ObjectUtil.isNotNull(company))
        {
            companyName=URLDecoder.decode(company.toString(), Charset.defaultCharset());
        }

        return AjaxResult.success(orderStatisticsService.getRevenueTrend(type,companyName));
    }

    /**
     * 获取月收入趋势
     *
     * @param type 类型
     * @return {@link AjaxResult }
     */
    @ApiOperation(value = "获取月营收趋势数据")
    @GetMapping("/getMonthlyRevenueTrend")
    public AjaxResult getMonthlyRevenueTrend(
            @ApiParam(value = "类型：3-近3个月，12-近12个月", required = true)
            @RequestParam(value = "type", defaultValue = "3") Integer type) {

        String companyName=null;
        Object company = ServletUtils.getRequest().getHeader("company");
        if(ObjectUtil.isNotNull(company))
        {
            companyName=URLDecoder.decode(company.toString(), Charset.defaultCharset());
        }

        return AjaxResult.success(orderStatisticsService.getMonthlyRevenueTrend(type,companyName));
    }

    /**
     * 获取每小时订单分配
     *
     * @return {@link AjaxResult }
     */
    @GetMapping("/hourly/distribution")
    public AjaxResult getHourlyOrderDistribution() {
        String companyName=null;
        Object company = ServletUtils.getRequest().getHeader("company");
        if(ObjectUtil.isNotNull(company))
        {
            companyName=URLDecoder.decode(company.toString(), Charset.defaultCharset());
        }
        List<HourlyOrderDistributionVO> distributionList = orderStatisticsService.getHourlyOrderDistribution(companyName);
        return AjaxResult.success(distributionList);
    }

    /**
     * 获得每小时收入分配
     *
     * @return {@link AjaxResult }
     */
    @ApiOperation(value = "获取时段营收分布")
    @GetMapping("/hourly/revenue")
    public AjaxResult getHourlyRevenueDistribution() {
        String companyName=null;
        Object company = ServletUtils.getRequest().getHeader("company");
        if(ObjectUtil.isNotNull(company))
        {
            companyName=URLDecoder.decode(company.toString(), Charset.defaultCharset());
        }
        List<HourlyRevenueDistributionVO> distributionList = orderStatisticsService.getHourlyRevenueDistribution(companyName);
        return AjaxResult.success(distributionList);
    }
} 